<?php
    $conn = new mysqli('localhost', 'root', 'root', 'sanxing');

    if ($conn -> connect_error) {
        die('登录失败');
    }

    // 接收前端的参数
    $username = $_POST['username'];
    
    $password = $_POST['password'];

    $sql = "SELECT * FROM `ceshi` WHERE `username`='$username' AND `password`='$password'";

    $res = $conn -> query($sql);

    $arr = array();
//解析数据
    $row = mysqli_fetch_assoc($res);

    if ($row) {
        echo json_encode(array(
            'state' => false,
            'msg' => '用户名已经被注册,请重新注册',
            'data'=>null
        ));
    } else {
       
           $sql2="INSERT INTO `ceshi`(`username`,`password`)VALUES('$username','$password')";
            $res2=$conn->query($sql2);

            if($res2){
                echo json_encode(array(
                    'state' => true,
                    'msg' => '注册成功',
                    'data'=>null
                ));
            }else{
                echo json_encode(array(
                    'state' => false,
                    'msg' => '注册失败',
                    'data'=>null
                ));
            }
        }
?>